package com.medicine.hbp.ui;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.medicine.hbp.adapter.VoteAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.VoteDetailsVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.MListView;
import com.medicine.hbp.R;

/**
 * 投票
 *
 * @author jindandan
 */
public class VoteActivity extends BaseActivity {
	@BindView(id = R.id.text_vote_nr)
	public TextView text_vote_nr;
	@BindView(id = R.id.list_vote)
	public MListView list_vote;
	@BindView(id = R.id.text_vote_title)
	public TextView text_vote_title;
	@BindView(id = R.id.image_vote)
	public ImageView image_vote;
	@BindView(id = R.id.btn_vote_yes, click = true)
	public Button btn_vote_yes;

	private VoteAdapter adapter;
	private VoteDetailsVo detailsVo=null;

	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_vote);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		text_vote_title.setVisibility(View.GONE);
		btn_vote_yes.setVisibility(View.GONE);
		initTop();
		httpVoteDetails();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.btn_vote_yes:
				if (!TextUtils.isEmpty(adapter.getoptionBoolean())) {
					httpConseSubmit(adapter.getoptionBoolean());
				} else {
					ToastUtil.showShort(aty, "你还没有选择！");
				}
				break;
			default:
				break;
		}
	}

	// private void initRido(final List<OptionListVo> optionList) {
	// vote_radio_group.removeAllViews();
	// for (int i = 0; i < optionList.size(); i++) {
	// RadioButton rad_btn = new RadioButton(aty);
	// RadioGroup.LayoutParams btnLayParams = new
	// RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT,
	// LayoutParams.WRAP_CONTENT);
	//
	// // btnLayParams.rightMargin = CommUtil.dip2px(context, 10);
	// rad_btn.setLayoutParams(btnLayParams);
	// rad_btn.setButtonDrawable(R.color.transparent);
	// // btn.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
	// rad_btn.setGravity(Gravity.CENTER | Gravity.LEFT);
	//
	// Drawable rightDrawable =
	// aty.getResources().getDrawable(R.drawable.vote_readio_selector);
	// rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(),
	// rightDrawable.getMinimumHeight());
	// Drawable buttmn =
	// aty.getResources().getDrawable(R.drawable.vote_item_back);
	// rad_btn.setCompoundDrawables(null, null, rightDrawable, buttmn);
	// rad_btn.setCompoundDrawablePadding(10);
	//
	// //
	// rad_btn.setTextColor(context.getResources().getColor(R.color.color_pay));
	// rad_btn.setPadding(10, 10, 10, 10);
	// rad_btn.setTextSize(16);
	// rad_btn.setText(optionList.get(i).optionMsg);
	// // View view=LinearLayout.inflate(aty, R.layout.vote_item, null);
	// // TextView textView=(TextView)
	// // view.findViewById(R.id.vote_item_name);
	// // RadioButton rad_btn=(RadioButton)
	// // view.findViewById(R.id.vote_item_btn);
	// // textView.setText("选项"+i);
	// vote_radio_group.addView(rad_btn);
	// }
	// vote_radio_group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	// {
	// @Override
	// public void onCheckedChanged(RadioGroup arg0, int arg1) {
	// ToastUtil.showShort(aty, optionList.get(arg1-1).optionMsg);
	// httpConseSubmit(optionList.get(arg1-1).optionId);
	// }
	// });
	// }

	/**
	 * 用户获取投票活动信息
	 */
	private void httpVoteDetails() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(this,HttpInterface.FIND_VOTE, params,
				new AppHttpCallBack<VoteDetailsVo>(VoteDetailsVo.class, aty) {
					@Override
					public void onSuccess(VoteDetailsVo result) {
						detailsVo = result;
						KJBitmap bitmap = new KJBitmap();
						bitmap.display(image_vote, result.voteInfo.voteBgImg);
						text_vote_title.setVisibility(View.VISIBLE);
						btn_vote_yes.setVisibility(View.VISIBLE);
						text_vote_title.setText(result.voteInfo.voteTitle);
						text_vote_nr.setText(result.voteInfo.voteMsg);
						adapter = new VoteAdapter(aty,
								result.voteInfo.optionList,
								result.voteInfo.voteIsRadio);
						list_vote.setAdapter(adapter);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	/**
	 * 用户提交投票结果
	 */
	private void httpConseSubmit(String voteId) {
		HttpParams params = new HttpParams();
		params.put("voteId", detailsVo.voteInfo.voteId);// 所在投票ID
		params.put("optionId", voteId);// 所投结果ID集合(以逗号割开)
		AppHttps.HttpPost(this,HttpInterface.ADD_CONSE, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						ToastUtil.showShort(aty, "投票成功");
						httpVoteDetails();
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("投票");
		topview.setLeftOnClick(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (LoginSp.getUserId(aty)!= 0) {
					StartIntent.startUser(aty,LoginSp.getUserId(aty));
				} else {
					StartIntent.startLogin(aty);
				}
			}
		});
		topview.setRightOnClick(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				StartIntent.startRelease(aty);
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);

	}
}
